import { defineComponent, reactive, ref, toRefs } from 'vue'
import { NForm, NFormItem, NModal, NSelect, SelectOption } from 'naive-ui'
import { Response } from '@/service/modules/common/types'
import { updateTableOwnerReq } from '@/service/modules/data-manage'
import { searchUserByUsernameReq } from '@/service/modules/user'

const ModTableOwnerModal = defineComponent({
  name: 'ModTableOwnerModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    datasourceId: {
      type: Number,
      required: true
    },
    dbName: {
      type: String,
      required: true
    },
    tableName: {
      type: String,
      required: true
    }
  },
  emits: ['modalShowChange', 'refreshList'],
  setup() {
    const state = reactive({
      form: {
        datasourceId: 0,
        dbName: '',
        tableName: '',
        owner: ''
      },
      positiveButtonDisabled: ref(false)
    })
    const loadingRef = ref(false)
    const optionsRef = ref<SelectOption[]>([])
    return {
      ...toRefs(state),
      loadingRef,
      optionsRef
    }
  },
  methods: {
    async onPositiveClick() {
      //提交修改库责任人请求
      this.positiveButtonDisabled = true
      this.form.datasourceId = this.datasourceId
      this.form.dbName = this.dbName
      this.form.tableName = this.tableName
      const res: Response<any> = await updateTableOwnerReq({
        ...this.form
      }).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshList')
        this.positiveButtonDisabled = false
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.form.datasourceId = 0
      this.form.dbName = ''
      this.form.tableName = ''
      this.form.owner = ''
    },
    async handleSearch(query: string) {
      if (!query.length) {
        this.optionsRef = []
        return
      }
      this.loadingRef = true
      const res = await searchUserByUsernameReq({ username: query }).catch(
        () => {
          this.loadingRef = false
        }
      )
      if (res.resp_code === 0) {
        this.optionsRef = res.data
      }
      this.loadingRef = false
    }
  },
  render() {
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={window.$t('dataManage.modOwner')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={window.$t('dialog.positiveText')}
        negative-text={window.$t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        onClose={this.onNegativeClick}
      >
        <NForm
          label-placement='top'
          require-mark-placement='right-hanging'
          label-width='auto'
        >
          <NFormItem path='owner' label={window.$t('dataManage.owner')}>
            <NSelect
              v-model:value={this.form.owner}
              filterable
              placeholder={window.$t('user.searchUser')}
              options={this.optionsRef}
              label-field='username'
              value-field='username'
              loading={this.loadingRef}
              clearable
              remote
              clear-filter-after-select
              onSearch={this.handleSearch}
            ></NSelect>
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default ModTableOwnerModal
